import 'package:flutter/material.dart';

class EnhancedPasswordField extends StatelessWidget {
  final TextEditingController controller;
  final String? errorText;
  final VoidCallback? onToggleVisibility;
  final bool obscureText;

  const EnhancedPasswordField({
    super.key,
    required this.controller,
    this.errorText,
    this.onToggleVisibility,
    this.obscureText = true,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        TextField(
          controller: controller,
          obscureText: obscureText,
          decoration: InputDecoration(
            labelText: '密码',
            suffixIcon: IconButton(
              icon: Icon(obscureText ? Icons.visibility : Icons.visibility_off),
              onPressed: onToggleVisibility,
            ),
            border: const OutlineInputBorder(),
            errorBorder: const OutlineInputBorder(
              borderSide: BorderSide(color: Colors.red, width: 1),
            ),
            focusedErrorBorder: const OutlineInputBorder(
              borderSide: BorderSide(color: Colors.red, width: 2),
            ),
          ),
        ),

        // ✅ 错误提示区域
        if (errorText != null) _buildErrorIndicator(errorText!),
      ],
    );
  }

  Widget _buildErrorIndicator(String errorText) {
    return Padding(
      padding: const EdgeInsets.only(top: 4),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(height: 1, color: Colors.red), // 红色下划线
          const SizedBox(height: 4),
          Text(
            errorText,
            style: const TextStyle(
              color: Colors.red,
              fontSize: 12,
              height: 1.2,
            ),
          ),
        ],
      ),
    );
  }
}
